package com.JobFind.logic.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


import com.JobFind.logic.IResumeLogic;
import com.JobFind.model.TbLog;
import com.JobFind.model.TbResume;
import com.JobFind.model.TbUser;
import com.JobFind.DAO.ILogDAO;
import com.JobFind.DAO.IResumeDAO;
import com.JobFind.DAO.IUserDAO;
import com.JobFind.DTO.ResumeDTO;
import com.JobFind.DTO.SimpleResumeDTO;
import com.JobFind.DTO.UserDTO;

@Component
public class ResumeLogic implements IResumeLogic{
	private IResumeDAO resumeDAO;
	@Resource
	private IUserDAO userDAO;

	public IResumeDAO getResumeDAO() {
		return resumeDAO;
	}

	@Resource
	public void setResumeDAO(IResumeDAO resumeDAO) {
		this.resumeDAO = resumeDAO;
	}


	@Override
	@Transactional
	public Integer addResume(Integer userId) throws Exception {
		Integer result = 0;
		try {
			ResumeDTO resumeDTO = new ResumeDTO();
			UserDTO userDTO = new UserDTO();
			userDTO.setUserId(userId);
			resumeDTO.setUserDTO(userDTO);
			result = this.resumeDAO.addResume(resumeDTO.toDAO());
		} catch (Exception e) {
		  e.printStackTrace();
		  result = 0;
		}
		return result;
	}

	@Override
	@Transactional
	public Integer updateResume(ResumeDTO resumeDTO, UserDTO userDTO)
			throws Exception {
		Integer result = 0;
		try {
			resumeDTO.setUserDTO(userDTO);
			result = this.resumeDAO.updateResume(resumeDTO.toDAO());
			this.userDAO.updateUser(userDTO.toDAO());
		} catch (Exception e) {
		  e.printStackTrace();
		  result = 0;
		}
		return result;
	}

	@Override
	@Transactional
	public String getResInfoByUserId(Integer userId) throws Exception {
		String result = "";
		SimpleResumeDTO listRresumeDTO= new SimpleResumeDTO();
		TbResume resumeList = null;
		try {
			resumeList = this.resumeDAO.getResInfoByUserId(userId);
			listRresumeDTO = resumeList.toSimDTO();
			result =  JSONArray.fromObject(listRresumeDTO).toString();
		} catch (Exception e) {
		  e.printStackTrace();
		  result = "";
		}
		return result;
	}

	@Override
	@Transactional
	public Integer getResumeIdByUserId(Integer userId) throws Exception {
		Integer result = 0;
		try {
			result = this.resumeDAO.getResumeIdByUserId(userId);
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}
	
}
